<?php
/**
 * Zend Framework Contribution
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Mwb
 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    1.0 Document.php 2012-05-01 Sergio DE VASCONCELOS
 */

require_once('Zend/Mwb/Dom/Abstract.php');
require_once('Zend/Mwb/Dom/PhysicalModels.php');

/**
 * MySQL Workbench document
 *
 * General entity which describes MWB document.
 * It implements document abstraction with a document level operations.
 *
 * @category   Zend
 * @package    Zend_Mwb
 */
class Zend_Mwb_Dom_Document extends Zend_Mwb_Dom_Abstract
{
	/**
	 * @var String
	 */
	protected $version;
	/**
	 * @var String
	 */
	//protected $id;
	//protected $diagrams;
	
	protected $physicalModels;
	
	/**
	 * return String $version
	 */
	public function getVersion() {
        return $this->version;
	}
	public function setVersion($version) {
		$this->version = $version;
		//$this->node->data->value->setAttribute("version", $version);
		return $this;
	}
	/**
	 * return Zend_Mwb_Dom_PhysicalModels
	 */
	public function getPhysicalModels() {
		// FIXME Dom
		return $this->physicalModels;
	}
	public function setPhysicalModels($physicalModels) {
		// FIXME Dom
		$this->physicalModels = $physicalModels;
		return $this;
	}
	
	/**
	 * (non-PHPdoc)
	 * @see Zend_Mwb_Dom_Abstract::parse()
	 */
	public function parse() {
		$dom = $this->node;
		
		// retrive version
		if ($dom->documentElement->hasAttribute('version')) {
			$this->version = $dom->documentElement->getAttribute('version');
		}

		// retrive physicalModel
		$xpath = new DOMXPath($dom);// $dom->ownerDocument
    	$element = $xpath->query("//data/value/value[@key='physicalModels']/value")->item(0);
    	if (isset($element)) {
    		$this->physicalModels = new Zend_Mwb_Dom_PhysicalModels($element, $this);
	    }
	}
	
	/**
	 * return DOMDocument
	 */
	public function create() {
		$domDocument = new DOMDocument('1.0');
		
		$dataElement = $domDocument->createElement('data');
		// --------------------------------------------------------------------
		$grt_formatAttr = $domDocument->createAttribute('grt_format');
		$grt_formatAttr->value = '2.0';
		$document_typeAttr = $domDocument->createAttribute('document_type');
		$document_typeAttr->value = 'MySQL Workbench Model';
		$versionAttr = $domDocument->createAttribute('version');
		$versionAttr->value = '1.4.2';
		
		$dataElement->appendChild($grt_formatAttr);
		$dataElement->appendChild($document_typeAttr);
		$dataElement->appendChild($versionAttr);
		$domDocument->appendChild($dataElement);
		
		
		$valueElement = $domDocument->createElement('value');
		// --------------------------------------------------------------------
		$typeAttr = $domDocument->createAttribute('type');
		$typeAttr->value = 'object';
		$struct_nameAttr = $domDocument->createAttribute('struct-name');
		$struct_nameAttr->value = 'workbench.Document';
		$idAttr = $domDocument->createAttribute('id');
		$idAttr->value = '8f7cb080-93b5-11e1-ae18-f8d1111101af';
		$struct_checksumAttr = $domDocument->createAttribute('struct-checksum');
		$struct_checksumAttr->value = '0x7131bf99';
		
		$valueElement->appendChild($typeAttr);
		$valueElement->appendChild($struct_nameAttr);
		$valueElement->appendChild($idAttr);
		$valueElement->appendChild($struct_checksumAttr);
		$domDocument->appendChild($valueElement);
		
		// $physicalModelsElement = Zend_Mwb_Element_PhysicalModels->create();
		// $valueElement->appendChild($physicalModelsElement);
			// $diagramsElement = Zend_Mwb_Element_Diagrams->create();
			// $valueElement->appendChild($diagramsElement);
			
			// $catalogsElement = Zend_Mwb_Element_Catalogs->create();
			// $valueElement->appendChild($catalogsElement);
		
		return $domDocument;
	}
}
